package com.ruicar.afs.cloud.channel.channelworkflow.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.channel.cardealer.common.Constants;
import com.ruicar.afs.cloud.channel.channelworkflow.condition.ChannelOnlineWorkFlowCondition;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskApproveRecord;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskApproveRecordService;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskDetailService;
import com.ruicar.afs.cloud.channel.channelworkflow.vo.WorkTaskApproveRecordVo;
import com.ruicar.afs.cloud.channel.online.condition.ChannelOnlineCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: sang jian
 * @date: 2020/7/29 19:38
 * @description:
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/approvalSchedule")
@Api(value = "approvalSchedule", description = "审批进度")
public class ApprovalScheduleController {


    private final WorkTaskApproveRecordService workTaskApproveRecordService;

    @PostMapping(value = "/getApprovalSchedule")
    @ApiOperation(value = "查询审批进度")
    public IResponse getApprovalScheduleByBizDataId(@RequestBody ChannelOnlineWorkFlowCondition condition) {
        //入参:bizid
        //返回任务名称,时间,备注,操作
        String bizDataId = condition.getBizDataId();
        List <String> approvalScheduleList =  new ArrayList<>();
        List <WorkTaskApproveRecordVo> workTaskApproveRecordVoList =  new ArrayList<>();

        List<WorkTaskApproveRecord> recordList = workTaskApproveRecordService.list(Wrappers.<WorkTaskApproveRecord>query().lambda()
                .eq(WorkTaskApproveRecord::getBizDataId, bizDataId)
                .orderByAsc(WorkTaskApproveRecord::getCreateTime)
        );

        if(recordList.size() > 0){
            for (WorkTaskApproveRecord record :recordList){
                WorkTaskApproveRecordVo workTaskApproveRecordVo = new WorkTaskApproveRecordVo();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String datetime = sdf.format(record.getCreateTime());

                if(Constants.START.equals(record.getApprovalOpinion())){
                    workTaskApproveRecordVo.setTime(datetime);
                    workTaskApproveRecordVo.setDetail(record.getApprovalOpinionDescribe() +" " + "提交");
                }else{
                    if(record.getRemark() != null){
                        workTaskApproveRecordVo.setTime(datetime);
                        workTaskApproveRecordVo.setDetail(record.getTaskName() + " " + "审核" + " " + record.getApprovalOpinionDescribe() + " " + record.getRemark());
                    }else{
                        workTaskApproveRecordVo.setTime(datetime);
                        workTaskApproveRecordVo.setDetail(record.getTaskName() + " " + "审核" + " " + record.getApprovalOpinionDescribe());
                    }
                }
                workTaskApproveRecordVoList.add(workTaskApproveRecordVo);
            }
        }

        return IResponse.success(workTaskApproveRecordVoList);

    }

}
